$(function () {
    toggleLoading(true);
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var originalHref = $('#doc-header a').attr('href');
    $('#doc-header a').attr('href', originalHref + '?hotel=' + currentHotel);
    originalHref = $('#doc-content a').attr('href');
    $('#doc-content a').attr('href', originalHref + '?hotel=' + currentHotel);
    toggleLoading(false);

    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
    }

    var requireLoginErrorHandler = function (error) {
        toggleLoading(false);
        if (typeof error.responseJSON === 'undefined') {
            toastr.error(lang('tip.system'));
            return;
        }

        if (error.responseJSON.status === 304) {
            $('#order-list-toggle').addClass('hidden');
            $('#doc-body').addClass('hidden');
            $('#doc-content').removeClass('hidden');
            return;
        }
        toastr.error(error.responseJSON.info);
    };

    var requestOrder = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Order/userItem', {
            'status': 0
        }, 'post', function (data) {
            toggleLoading(false);
            var orderInfos = data.data.order;
            orderInfos.reverse();
            var ordersHTML = '';
            if (orderInfos.length === 0) {
                ordersHTML = $('#templateOrderEmpty').clone().html();
            } else {
                var templateOrderHTML = $($('#templateOrder').clone().html());
                $.each(orderInfos, function (key, orderInfo) {
                    templateOrderHTML.attr('href', '/Home/OrderPage/index?hotel=' + currentHotel + '&no=' + orderInfo.orderNumber);
                    templateOrderHTML.find('.order-cover').attr('data-original', orderInfo.roomTypeCoverImageUrl);
                    var userLoginId = orderInfo.user[0].userLoginId;
                    var bookLoginId = orderInfo.orderRoom;
                    for(var i=0;i<bookLoginId.length;i++){
                        var loginId = bookLoginId[i].userLoginId;
                        if(loginId == userLoginId){
                            var realStatusName = bookLoginId[i].statusName;
                        }
                    }
                    var currentOrderRefundStatus = parseInt(orderInfo.refundStatus);
                    if (currentOrderRefundStatus === 1) {
                        realStatusName += ' -'+lang('applyRefund');
                    }
                    if (currentOrderRefundStatus === 3) {
                        realStatusName += ' -'+lang('rejectRefund');
                    }
                    templateOrderHTML.find('.order-status').html(realStatusName);
                    templateOrderHTML.find('.order-hotel-name').html(orderInfo.hotelName);
                    templateOrderHTML.find('.order-room-type').html(orderInfo.roomTypeName);
                    templateOrderHTML.find('.order-room-name').html(orderInfo.roomName);
                    templateOrderHTML.find('.order-number').html(orderInfo.orderNumber);
                    templateOrderHTML.find('.order-price').html(orderInfo.price);
                    templateOrderHTML.find('.order-checkin-date').html(orderInfo.startDate);
                    templateOrderHTML.find('.order-checkout-date').html(orderInfo.endDate);
                    if (orderInfo.hour > 0) {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startdate);
                        templateOrderHTML.find('.order-checkout-date').addClass('hidden');
                        templateOrderHTML.find('.to').addClass('hidden');
                        templateOrderHTML.find('.order-night').html('('+orderInfo.hour+lang('hour')+')');
                    } else if(orderInfo.month > 0) {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startDate);
                        templateOrderHTML.find('.to').removeClass('hidden');
                        templateOrderHTML.find('.order-checkout-date').removeClass('hidden').html(orderInfo.endDate);
                        templateOrderHTML.find('.order-night').html('('+orderInfo.month+lang('month')+')');
                    } else {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startDate);
                        templateOrderHTML.find('.to').removeClass('hidden');
                        templateOrderHTML.find('.order-checkout-date').removeClass('hidden').html(orderInfo.endDate);
                        templateOrderHTML.find('.order-night').html('('+orderInfo.night+lang('nights')+')');
                    }
                    ordersHTML += templateOrderHTML.prop('outerHTML');
                });
            }
            $('#order-valid').html(ordersHTML);
            $('.order-cover').lazyload({
                skip_invisible: true
            });
        }, requireLoginErrorHandler);
    };

    var requestInvalidOrder = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Order/userItem', {
            'status': 1
        }, 'post', function (data) {
            toggleLoading(false);
            var orderInfos = data.data.order;
            orderInfos.reverse();
            var ordersHTML = '';
            if (orderInfos.length === 0) {
                ordersHTML = $('#templateOrderEmpty').clone().html();
            } else {
                var templateOrderHTML = $($('#templateOrderInvalid').clone().html());
                $.each(orderInfos, function (key, orderInfo) {
                    templateOrderHTML.attr('href', '/Home/OrderPage/index?hotel=' + currentHotel + '&no=' + orderInfo.orderNumber);
                    templateOrderHTML.find('.order-cover').attr('data-original', orderInfo.roomTypeCoverImageUrl);
                    var userLoginId = orderInfo.user[0].userLoginId;
                    var bookLoginId = orderInfo.orderRoom;
                    for(var i=0;i<bookLoginId.length;i++){
                        var loginId = bookLoginId[i].userLoginId;
                        if(loginId == userLoginId){
                            var realStatusName = bookLoginId[i].statusName;
                        }
                    }
                    templateOrderHTML.find('.order-status').html(realStatusName);
                    templateOrderHTML.find('.order-hotel-name').html(orderInfo.hotelName);
                    templateOrderHTML.find('.order-room-type').html(orderInfo.roomTypeName);
                    templateOrderHTML.find('.order-room-name').html(orderInfo.roomName);
                    templateOrderHTML.find('.order-number').html(orderInfo.orderNumber);
                    templateOrderHTML.find('.order-price').html(orderInfo.price);
                    if (orderInfo.hour > 0) {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startdate);
                        templateOrderHTML.find('.order-checkout-date').addClass('hidden');
                        templateOrderHTML.find('.to').addClass('hidden');
                        templateOrderHTML.find('.order-night').html('('+orderInfo.hour+lang('hour')+')');
                    } else if (orderInfo.month > 0) {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startDate);
                        templateOrderHTML.find('.to').removeClass('hidden');
                        templateOrderHTML.find('.order-checkout-date').removeClass('hidden').html(orderInfo.endDate);
                        templateOrderHTML.find('.order-night').html('('+orderInfo.month+lang('month')+')');
                    } else {
                        templateOrderHTML.find('.order-checkin-date').html(orderInfo.startDate);
                        templateOrderHTML.find('.to').removeClass('hidden');
                        templateOrderHTML.find('.order-checkout-date').removeClass('hidden').html(orderInfo.endDate);
                        templateOrderHTML.find('.order-night').html('('+orderInfo.night+lang('nights')+')');
                    }
                    
                    ordersHTML += templateOrderHTML.prop('outerHTML');
                });
            }
            
            $('#order-invalid').html(ordersHTML);
            $('.order-cover').lazyload({
                skip_invisible: true
            });
        }, requireLoginErrorHandler);
    };
    requestOrder();

//  $('#order-list-toggle').on('click', function (e) {
//      e.preventDefault();
//      var submitBtn = $(this);
//      var status = submitBtn.data('status');
//      if (status === 'invalid') {
//          $('#order-invalid').addClass('hidden');
//          $('#order-valid').removeClass('hidden');
//          submitBtn.data('status', 'valid');
//          submitBtn.find('.iconfont').removeClass('receipt-icon');
//      } else {
//          if (status === undefined) {
//              requestInvalidOrder();
//          }
//          $('#order-invalid').removeClass('hidden');
//          $('#order-valid').addClass('hidden');
//          submitBtn.data('status', 'invalid');
//          submitBtn.find('.iconfont').addClass('receipt-icon');
//      }
//  });
    $('#doc-header').on('click','.tabs-order',function(e){
        e.preventDefault();
        var submitBtn = $(this);
        submitBtn.removeClass('before').addClass('active');
        submitBtn.siblings().removeClass('active').addClass('before');
        var status = submitBtn.attr('data');
        if (status === 'valid') {
            $('#order-invalid').addClass('hidden');
            $('#order-valid').removeClass('hidden');
            requestOrder();
        } else {
            $('#order-invalid').removeClass('hidden');
            $('#order-valid').addClass('hidden');
            requestInvalidOrder();
        }
    })

    $('#order-valid').on('click', '.order-refresh', function (e) {
        e.preventDefault();
        requestOrder();
    });
    $('#order-invalid').on('click', '.order-refresh', function (e) {
        e.preventDefault();
        requestInvalidOrder();
    });
});
